var jsonfile = require('jsonfile')
var file = './data.json'
let fs = require("fs");
var Mock = require('mockjs')

function injectContentTofileUntil(fileSizeInMega) {

    let obj = jsonfile.readFileSync(file)
    console.log(obj)
    if(!obj){
        obj=[]
    }

    for (let index = 0; index < 1000; index++) {
        obj.push({
            name: Mock.Random.cname(),
            country: Mock.Random.county(true),
        })
    }

    jsonfile.writeFileSync(file, obj)
    let fileSizeInMegabytes = getFileSizeInMega(file)
    if (fileSizeInMegabytes >= fileSizeInMega) {
        return true
    } else {
        return false
    }
}

function getFileSizeInMega(filePath) {
    let stats = fs.statSync(file)
    let fileSizeInBytes = stats.size
    let fileSizeInMegabytes = fileSizeInBytes / 1000000.0
    return fileSizeInMegabytes
}

while (!injectContentTofileUntil(1)) {
    console.log(getFileSizeInMega(file));
}